import { createRouter, createWebHistory } from "vue-router";
import routes from "./routes";
import nProgress from "@/utils/nprogress";
const router = createRouter({
  history: createWebHistory(),
  routes,
});
// 路由守卫拦截
router.beforeEach((to, from, next) => {
  nProgress.start();
  if (to.meta.title) {
    document.title = to.meta.title as string;
  }
  // 权限控制
  const token = localStorage.getItem("token");
  if (to.meta.requiresAuth && !token) {
    next("/login");
    return;
  }
  next();
});
router.afterEach(() => {
  nProgress.done();
});

export default router;
